class Admin::GamesController < AdminController
  def index
    @games = Game.all
    if params[:search_key].presence
      @games = @games.joins("INNER JOIN teams master ON games.master_id = master.id").joins("INNER JOIN teams guest ON games.guest_id = guest.id").where("master.name LIKE ? OR guest.name LIKE ?", "%#{params[:search_key]}%", "%#{params[:search_key]}%")
    end
    @games = @games.order("begin_at DESC").page params[:page]
  end

  def new
    @game = Game.new
  end

  def create
    @game = Game.new game_params
    if @game.save
      if @game.master_or_guest
        team = @game.master
      else
        team = @game.guest
      end
      # registration_ids = User.joins(:plans).where("plans.team_id = ? AND state = 0", team.id).select("users.registration_id").pluck("users.registration_id")
      registration_ids = User.select("registration_id").where("registration_id != ''").pluck("registration_id")
      jpush = JPush::Client.new(Figaro.env.jpush_app_key, Figaro.env.jpush_secret)
      audience = JPush::Push::Audience.new.set_registration_id(registration_ids)
      push_payload = JPush::Push::PushPayload.new(
        platform: 'all',
        audience: audience,
        notification: '有新的比赛，请计划.',
        message: "有#{team.name}的新比赛，请计划."
      )
      jpush.pusher.push(push_payload)
      redirect_to admin_games_path, notice: "保存成功！"
    else
      render :new
    end
  end

  def edit
    @game = Game.find params[:id]
  end

  def update
    @game = Game.find params[:id]
    pre_state = @game.pending?
    if @game.update game_params
      if pre_state && (@game.winned? || @game.losed? || @game.winned_half? || @game.losed_half? || @game.tie?)
        @bets = Bet.where(game_id: @game.id)
        @bets.each do |bet|
          if @game.winned?
            if bet.master_or_guest
              bet.update(state: :win, total_fee: bet.money * @game.master_index)
            else
              bet.update(state: :win, total_fee: bet.money * @game.guest_index)
            end
            bet.plan.update(state: :completed)
          elsif @game.losed?
            bet.update(state: :lose, total_fee: bet.money)
          elsif @game.winned_half?
            if bet.master_or_guest
              bet.update(state: :winned_half, total_fee: bet.money * @game.master_index * 0.5)
            else
              bet.update(state: :winned_half, total_fee: bet.money * @game.guest_index * 0.5)
            end
            bet.plan.update(state: :completed)
          elsif @game.losed_half?
            if bet.master_or_guest
              bet.update(state: :losed_half, total_fee: bet.money * 0.5)
            else
              bet.update(state: :losed_half, total_fee: bet.money * 0.5)
            end
          elsif @game.tie?
            bet.update(state: :tie, total_fee: 0)
          end
        end
      end
      redirect_to admin_games_path, notice: "更新成功！"
    else
      render :edit
    end
  end
  
  def destroy
    @game = Game.find params[:id]
    if @game.destroy
      redirect_to admin_games_path, notice: "删除成功."
    else
      redirect_to admin_games_path, notice: "删除失败."
    end
  end

  private

  def game_params
    params.require(:game).permit(:league_game_id, :begin_at, :master_id, :master_score, :guest_id, :guest_score, :master_index, :guest_index, :sb, :propose, :remark, :master_or_guest, :state)
  end
end
